//
//  XMGRecomendViewController.m
//  01 baisi
//
//  Created by 檀志文 on 16/4/9.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "XMGRecomendViewController.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import "XMGXMGRecomendCell.h"
#import <MJExtension.h>
#import "XMGRecomendCategories.h"
#import "XMGUserCell.h"
#import "XMGUser.h"
#import <MJRefresh.h>

#define ss self.categories[self.categoryTb.indexPathForSelectedRow.row]
@interface XMGRecomendViewController ()<UITableViewDataSource,UITableViewDelegate>



@property (nonatomic, strong) NSArray *categories;


// 左边的类别表格
@property (weak, nonatomic) IBOutlet UITableView *categoryTb;

//右边的用户表格
@property (weak, nonatomic) IBOutlet UITableView *userTb;

@end

@implementation XMGRecomendViewController

static NSString *const XMGCategoryID=@"category";
static NSString *const XMGCategoryUser=@"user";


- (void)setupTableView{
    //    注册
    [self.categoryTb registerNib:[UINib nibWithNibName:NSStringFromClass([XMGXMGRecomendCell class]) bundle:nil] forCellReuseIdentifier:XMGCategoryID];
    
    [self.userTb registerNib:[UINib nibWithNibName:NSStringFromClass([XMGUserCell class]) bundle:nil] forCellReuseIdentifier:XMGCategoryUser];
    
    self.categoryTb.rowHeight = 75;
    self.categoryTb.bounces =NO;
    self.categoryTb.showsVerticalScrollIndicator =NO;
    self.categoryTb.backgroundColor = XMGGlobalBg;
    
    
//    self.userTb.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.categoryTb.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.userTb.contentInset = self.categoryTb.contentInset;
    self.userTb.rowHeight = 140;
    self.userTb.backgroundColor =XMGGlobalBg;
//    self.userTb.bounces =NO;
    self.userTb.showsVerticalScrollIndicator =NO;
//    self.userTb.tableFooterView = 
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //控件初始化
    [self setupTableView];
    
//    添加刷新控件
    [self setupRefresh];
    
    
    self.title =@"推荐关注";
    self.view.backgroundColor = XMGGlobalBg;
    
    //显示指示器
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"category";
    params[@"c"] = @"subscribe";
    //发送请求

    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php"   parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.categories = [XMGRecomendCategories mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        [self.categoryTb reloadData];

        
        //隐藏指示器
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //显示失败信息
        [SVProgressHUD showErrorWithStatus:@"加载推荐信息失败！"];
    }];
}
//刷新
-(void)setupRefresh{
    self.userTb.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];}

-(void)loadData{
    //发送请求给服务器，加载右侧的数据
    
    XMGRecomendCategories *c= ss;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = @"list";
    params[@"c"] = @"subscribe";
    params[@"category_id"] =c.id;
    params[@"page"] =@"2";
    
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray  *users=[XMGUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        [c.users addObjectsFromArray:users];
        
        [self.userTb reloadData];
        //让底部控件结束刷新
        [self.userTb.footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView ==self.categoryTb) {
       return self.categories.count;
    }else{
        
        NSInteger count = [ss users].count;
//        XMGRecomendCategories *c = ss;
        
        //控制每次刷新右边表格
        self.userTb.footer.hidden =(count==0);
        return count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.categoryTb) {
        XMGXMGRecomendCell *cell = [tableView dequeueReusableCellWithIdentifier:XMGCategoryID];
        
        cell.category = self.categories[indexPath.row];
        
        return cell;
    }else{
        XMGUserCell *cell = [tableView dequeueReusableCellWithIdentifier:XMGCategoryUser];
//         XMGRecomendCategories *c = ss;
        cell.users = [ss users][indexPath.row];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XMGRecomendCategories *c= self.categories[indexPath.row];
    if (c.users.count) {
        //
        [self.userTb reloadData];
    }else{
        //赶紧刷新表格，目的不让用户看到上一个类别的数据
        
        [self.userTb reloadData];
        //发送请求给服务器，加载右侧的数据
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] = @"list";
        params[@"c"] = @"subscribe";
        params[@"category_id"] =c.id;
        
        [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSArray  *users=[XMGUser mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            [c.users addObjectsFromArray:users];
            
            
            
            [self.userTb reloadData];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
    }
}
/**
 *  目前只能显示1页数据
    重复发送请求怎么处理
    网络慢带来的细节问题
 */


@end
